package com.nc.delivery_project.server.service;

import com.nc.delivery_project.server.domain.Schedule;
import com.nc.delivery_project.server.domain.path_finder.old.DirectedGraph;
import com.nc.delivery_project.server.domain.path_finder.graph.Dijkstra;
import com.nc.delivery_project.server.domain.path_finder.graph.Edge;
import com.nc.delivery_project.server.domain.path_finder.graph.Graph;
import com.nc.delivery_project.server.domain.path_finder.graph.Node;

import java.util.*;

/**
 * Created by Даниил on 03.02.2015.
 */
public class LogisticService {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        Date startDate = cal.getTime();
        cal.add(Calendar.MONTH, 6);
        Date endDate = cal.getTime();

        List<Schedule> scheduleList = new ScheduleService().read(startDate, endDate);

        Set<Node> nodes = new HashSet<>();
        Set<Edge> edges = new HashSet<>();
        for (Schedule schedule : scheduleList) {
            Node node1 = new Node(schedule.getOrigin().getDeptId());
            Node node2 = new Node(schedule.getDestination().getDeptId());
            nodes.add(node1);
            nodes.add(node2);

            double hours = (schedule.getArrivalDate().getTime() - schedule.getDepartureDate().getTime())/(60*60*1000);

            double cost = schedule.getPrimeCost() / schedule.getSellingRate();

            edges.add(new Edge(node1, node2, hours, cost));
        }

        Graph graph = new Graph(nodes, edges);
        Dijkstra dijkstra = new Dijkstra(graph);
        Edge.WEIGHT_MODE = Edge.COST_WEIGHT_MODE;
        dijkstra.execute(new Node(291));
        LinkedList<Node> path = dijkstra.getPath(new Node(350));
    }

    public List<Integer> computeShortestPath(int sourceId, int destId) {
//        Calendar cal = Calendar.getInstance();
//        Date startDate = cal.getTime();
//        cal.add(Calendar.MONTH, 6);
//        Date endDate = cal.getTime();
//
////        ScheduleService scheduleService = new ScheduleService();
////        List<Schedule> scheduleList = scheduleService.read(startDate, endDate);
//
//        List<Schedule> scheduleList = FillDb.createSchedules(500, false);
//
//        List<Schedule> filteredList = new ArrayList<>();
//        for (Schedule schedule : scheduleList) {
//            if (schedule.getDepartureDate().compareTo(startDate) >= 0
//                    && schedule.getArrivalDate().compareTo(endDate) <= 0) {
//                filteredList.add(schedule);
//            }
//        }
//
//        DirectedGraph<Integer> graph = new DirectedGraph<>();
//
//
//        for (Schedule schedule : filteredList) {
//            double length = (schedule.getArrivalDate().getTime() - schedule.getDepartureDate().getTime())/(60*60*1000);
//            graph.addEdge(schedule.getOrigin().getDeptId(), schedule.getDestination().getDeptId(), length);
//        }
//
//        Map<Integer, Double> shortestPaths = Dijkstra.shortestPaths(graph, 291);

        return null;
    }

    private void computeShortestPath(int sourceId, int destId, DirectedGraph<Integer> graph) {

    }

    public List<Integer> computeCheapestPath(int sourceId, int destId) {
        return null;
    }

}
